package com.jiinfo.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jiinfo.util.StringUtils;
import com.jiinfo.util.UUIDUtils;

public class Role extends Model<Role>{

	private static final long serialVersionUID = 3035969687889500168L;
	public static final Role dao = new Role();

	public List<Record> getRolesByUserId(String userId){
		String sql="SELECT r.* FROM t_role r "
				+ "INNER JOIN t_user_role ur on r.id=ur.roleId and ur.removed='0' "
				+ "where r.removed='0' and ur.userId=?";

		return Db.find(sql,userId);
	}
	
	
	public Page<Record> pagequery(Integer pageNumber,Integer pageSize,Record record){
		StringBuffer sqlExceptSelect=new StringBuffer("from t_role t left join t_user u on t.creator=u.id where t.removed='0' ");
		List<Object> list=new ArrayList<Object>();
		
		
		if(!StringUtils.isEmpty(record.getStr("searchContent"))){
			sqlExceptSelect.append(" and t.name like ?");
			list.add("%"+record.getStr("searchContent")+"%");
		}
		
		sqlExceptSelect.append(" order by t.updateTime desc");
		
		if(pageNumber==null){
			pageNumber=1;
		}
		if(pageSize==null){
			pageSize=10;
		}
		return Db.paginate(pageNumber,pageSize,"select t.*,u.username creatorName",sqlExceptSelect.toString(),list.toArray());
	}
	
	public Role add(Role vo,String[] cks,Record user){
		Role role=new Role();
		role.set("id", UUIDUtils.getUUID());
		role.set("createTime", new Date());
		role.set("creator", user.getStr("id"));
		role.set("updateTime", new Date());
		role.set("updator", user.getStr("id"));
		role.set("removed", "0");
		
		role.set("name", vo.getStr("name"));
		role.set("code", vo.getStr("code"));
		
		role.save();
		
		if(cks!=null){
			//资源角色新增
			for (String ck : cks) {
				RoleResource roleResource=new RoleResource();
				roleResource.set("id", UUIDUtils.getUUID());
				roleResource.set("createTime", new Date());
				roleResource.set("creator", user.getStr("id"));
				roleResource.set("updateTime", new Date());
				roleResource.set("updator", user.getStr("id"));
				roleResource.set("removed", "0");
				
				roleResource.set("roleId", role.getStr("id"));
				roleResource.set("resourceId", ck);
				
				roleResource.save();
			}
		}
		
		return role;
	}
	
	public Role edit(Role vo,String[] cks,Record user){
		Role role=Role.dao.findById(vo.getStr("id"));
		role.set("updateTime", new Date());
		role.set("updator", user.getStr("id"));
		
		role.set("name", vo.getStr("name"));
		role.set("code", vo.getStr("code"));
		
		role.update();
		
		//先删除之前的资源角色
		RoleResource.dao.deleteRoleResourceByRoleId(role.getStr("id"));
		if(cks!=null){
			//资源角色编辑
			for (String ck : cks) {
				RoleResource roleResource=new RoleResource();
				roleResource.set("id", UUIDUtils.getUUID());
				roleResource.set("createTime", new Date());
				roleResource.set("creator", user.getStr("id"));
				roleResource.set("updateTime", new Date());
				roleResource.set("updator", user.getStr("id"));
				roleResource.set("removed", "0");
				
				roleResource.set("roleId", role.getStr("id"));
				roleResource.set("resourceId", ck);
				
				roleResource.save();
			}
		}
		
		return role;
	}
	
	public void delete(String id){
		Role.dao.deleteById(id);
		RoleResource.dao.deleteRoleResourceByRoleId(id);
		UserRole.dao.deleteUserRoleByRoleId(id);
	}
	
	public List<Record> getAll(){
		String sql="select * from t_role r where r.removed='0' ";
		return Db.find(sql);
	}
	
}
